package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.service.impl.AdminCouponServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponController extends HttpServlet {
    AdminCouponService adminCouponService = new AdminCouponServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replaced = requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
        if ("create".equals(replaced)) {
            create(req, resp);
        } else if ("delete".equals(replaced)) {
            delete(req, resp);
        } else if ("update".equals(replaced)) {
            update(req, resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String readLine = req.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(readLine, MarketCoupon.class);
        Object o = null;
        if (marketCoupon.getLimit() > 127) {
            o = ResponseUtil.fail(MarketConstant.WRONG_COUPONSETTIME, "一个最多领127张喔,不能逮着一家薅");
        }
            MarketCoupon coupon = adminCouponService.create(marketCoupon);

            int couponId = coupon.getId();
            //couponId==-1 说明领券相对天数小于等于0
            if (couponId == -1) {
                o = ResponseUtil.fail(MarketConstant.WRONG_COUPONDAYS, "优惠券天数不能为0");
            } else if (coupon.getId() == -2) {
                o = ResponseUtil.fail(MarketConstant.WRONG_COUPONENDTIME, "不想发可以不发");
            } else if (couponId == -3) {
                o = ResponseUtil.fail(MarketConstant.WRONG_COUPONSETTIME, "请给优惠券指定正确的优惠时间区间");
            } else {
                o = ResponseUtil.ok(coupon);
            }
            resp.getWriter().write(JacksonUtil.getObjectMapper().writeValueAsString(o));
        }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        String marketCouponJson = req.getReader().readLine();
        MarketCoupon marketCoupon = objectMapper.readValue(marketCouponJson, MarketCoupon.class);
        if (adminCouponService.delete(marketCoupon) == 1) {
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().write(s);
            return;
        }
        Object fail = ResponseUtil.fail(803, "删除失败");
        String s = JacksonUtil.getObjectMapper().writeValueAsString(fail);
        resp.getWriter().write(s);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        MarketCoupon ret = adminCouponService.update(marketCoupon);

        Object ok = ResponseUtil.ok(ret);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
        if ("list".equals(op)) {
            list(req, resp);
        } else if ("read".equals(op)) {
            read(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");

        String type = req.getParameter("type");
        

        String status = req.getParameter("status");


        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketCoupon> couponList = adminCouponService.list(page, limit, name, type, status, sort, order);
        Object object = ResponseUtil.okList(couponList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(object);
        resp.getWriter().println(s);
    }

    private void read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.获取请求
        String idStr = req.getParameter("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (Exception e) {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.fail(801, "数字转换异常")));
        }
        //2.业务逻辑
        MarketCoupon marketCoupon = adminCouponService.read(id);

        //3.返回响应
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketCoupon)));

    }
}
